<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class shop_uqcms extends control
{
    function __construct()
    {
        $this->spid = $_SESSION['shop']['spid'];
        parent::__construct();
    }
    public function index()
    {
        $row = $this->module('shop')->get_one($this->spid);
        $this->assign('row', $row);
        $order_num = $this->db->get_one("select count(*) as total from " . table('refund') . " where shop_id = " . $this->spid . ' and status = 1');
        $num_arr['refund'] = $order_num['total'];
        $pay_time = strtotime(date('Y-m-d 00:00:00', time()));
        $price_num = $this->db->get_one("select SUM(total_price) as sum from " . table('order_info') . " where shop_id = " . $this->spid . " and pay_time > " . $pay_time);
        $num_arr['order_price'] = isset($price_num['sum']) ? $price_num['sum'] : 0.0;
        $order_num = $this->db->get_one("select count(*) as total from " . table('order_info') . " where shop_id = " . $this->spid . ' and addtime > ' . $pay_time);
        $num_arr['order_num'] = $order_num['total'];
        $order_num = $this->db->get_one("select count(*) as total from " . table('order_info') . " where shop_id = " . $this->spid . ' and order_status = 2');
        $num_arr['order_send'] = $order_num['total'];
        $this->assign('status_num', $num_arr);
        $this->display();
    }
    public function base()
    {
        $this->assign('row', $this->module('shop')->get_one($this->spid));
        $this->display();
    }
    public function base_save()
    {
        if ($_POST) {
            $setsql['main_seller'] = $_POST['main_seller'];
            $setsql['mobile'] = isset($_POST['mobile']) ? $_POST['mobile'] : '';
            P('prov', '省份不能为空');
            $setsql['email'] = isset($_POST['email']) ? $_POST['email'] : '';
            P('prov', '省份不能为空');
            $setsql['qq'] = isset($_POST['qq']) ? $_POST['qq'] : '';
            P('prov', '省份不能为空');
            $setsql['ww'] = isset($_POST['ww']) ? $_POST['ww'] : '';
            P('prov', '省份不能为空');
            $setsql['prov'] = P('prov', '省份不能为空');
            $setsql['city'] = P('city', '城市不能为空');
            $setsql['dist'] = P('dist', '县区不能为空');
            $setsql['address'] = P('address');
            $setsql['contact'] = P('contact');
            $setsql['intro'] = P('intro');
            $name = P('name', '店铺名称不能为空');
            $setsql['name'] = $name;
            $query = $this->db->update(table('shop'), $setsql, 'id = ' . $this->spid);
            if ($query) {
                $_SESSION['shop']['name'] = $name;
                $arr['error'] = '0';
                $arr['msg'] = '保存成功';
                echo json_encode($arr);
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function map()
    {
        $this->assign('row', $this->module('shop')->get_one($this->spid));
        $this->display();
    }
    public function ajax_map()
    {
        $setsql['lng'] = P('lng', '经度不能为空');
        $setsql['lat'] = P('lat', '纬度不能为空');
        $query = $this->db->update(table('shop'), $setsql, 'id = ' . $this->spid);
        if ($query) {
            right_json();
        } else {
            error_json('保存失败');
        }
    }
    public function return_address()
    {
        $row = $this->db->get_one("select * from " . table('shop_address') . " where spid = " . $this->spid);
        if (isset($row['id'])) {
            $addr = array('prov' => $row['prov'], 'city' => $row['city'], 'dist' => $row['dist']);
            $addr_row = $this->module('address')->get_cn($addr);
            $row['prov_cn'] = @$addr_row[$row['prov']]['name'];
            $row['city_cn'] = @$addr_row[$row['city']]['name'];
            $row['dist_cn'] = @$addr_row[$row['dist']]['name'];
            $this->assign('row', $row);
        }
        $this->display();
    }
    function return_address_save()
    {
        if ($_POST) {
            $setarr['name'] = P('name', '姓名不能为空');
            $setarr['mobile'] = P('mobile', '电话不能为空');
            $setarr['prov'] = P('prov', '省份不能为空');
            $setarr['city'] = P('city', '城市不能为空');
            $setarr['dist'] = P('dist', '县区不能为空');
            $setarr['address'] = P('address', '详细地址不能为空');
            $setarr['zip'] = P('zip');
            $setarr['explain'] = P('explain');
            $row = $this->db->get_one("select id,spid from " . table('shop_address') . " where spid = " . $this->spid);
            if ($row['id']) {
                $query = $this->db->update(table('shop_address'), $setarr, 'spid =' . $this->spid);
            } else {
                $setarr['spid'] = $this->spid;
                $setarr['addtime'] = time();
                $query = $this->db->add(table('shop_address'), $setarr);
            }
            if ($query) {
                $arr['error'] = '0';
                echo json_encode($arr);
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function auth()
    {
        $row = $this->db->get_one("select id,uid from " . table('shop') . " where id = " . $this->spid);
        if ($row['id']) {
            $arow = $this->db->get_one("select * from " . table('shop_auth') . " where uid = " . $row['uid']);
            if ($arow['id'] && $arow['status'] === '1') {
                $arow['persion_id_code'] = substr_replace(@$arow['persion_id_code'], '******', 4, 5);
                $arow['company_id_code'] = substr_replace(@$arow['company_id_code'], '******', 4, 5);
                $arow['auth_time'] = _date($arow['auth_time']);
                $this->assign('row', $arow);
            }
        }
        $this->display();
    }
    function service()
    {
        $this->display();
    }
    function service_save()
    {
        if ($_POST) {
        } else {
            error_json('提交错误');
        }
    }
    function service_time()
    {
        $result = $this->db->get_all("select * from " . table('shop_service_time') . " where shop_id = " . $this->spid . " order by week asc");
        if ($result) {
            foreach ($result as $v) {
                $warr[$v['week']] = $v;
            }
        }
        for ($i = 1; $i <= 7; $i++) {
            if (isset($warr[$i])) {
                $v['t1'] = $this->daytime($warr[$i]['time1']);
                $v['t2'] = $this->daytime($warr[$i]['time2']);
                $v['t3'] = $this->daytime($warr[$i]['time3']);
                $v['t4'] = $this->daytime($warr[$i]['time4']);
            }
            $v['wkid'] = $i;
            $data[] = $v;
            unset($v);
        }
        if (isset($data)) {
            $this->assign('data', $data);
        }
        $this->display();
    }
    function service_time_save()
    {
        if ($_POST) {
            $spid = $this->spid;
            $query = $this->db->delete(table('shop_service_time'), 'shop_id = ' . $spid);
            $time = isset($_POST['time']) ? $_POST['time'] : '';
            $sql = '';
            foreach ($time as $k => $v) {
                $arr['shop_id'] = $this->spid;
                if ($v['t1'] != '' && $v['t2'] != '' || $v['t3'] != '' && $v['t4'] != '') {
                    $t1 = $this->wktime(strtotime($v['t1']));
                    $t2 = $this->wktime(strtotime($v['t2']));
                    $t3 = $this->wktime(strtotime($v['t3']));
                    $t4 = $this->wktime(strtotime($v['t4']));
                    $sql .= "(" . $spid . ", " . $k . ", " . $t1 . ", " . $t2 . ", " . $t3 . ", " . $t4 . "),";
                }
            }
            if (!empty($sql)) {
                $sql = rtrim($sql, ',');
                $query2 = $this->db->query("insert into " . table("shop_service_time") . " (`shop_id`,`week`,`time1`,`time2`,`time3`,`time4`) values " . $sql . ";");

                if ($query && $query2) {
                    right_json();
                } else {
                    error_json('保存失败');
                }
            } else {
                error_json('提交错误');
            }
        } else {
            error_json('提交错误');
        }
    }
    private function wktime($time)
    {
        return $time - strtotime(date('Ymd'));
    }
    private function daytime($time_diff)
    {
        $time = strtotime(date('Ymd'));
        $total = $time + $time_diff;
        return date("H:i", $total);
    }
}